package com.heavyWeather.observer
{
	import com.heavyWeather.observer.Collection;
	import com.heavyWeather.observer.IFCollection;
	import com.heavyWeather.observer.IFIterator;
	
	public class Subject implements IFSubject
	{
		private var list:IFCollection;
		private var changed:Boolean = new Boolean();
		
		public function addObserver(observer:IFObserver):Boolean
		{
			if(this.list == null) this.clearObservers();
			var boo:Boolean = this.list.addItem(observer);
			return boo;
		}
		
		public function getObservers():IFCollection
		{
			if(this.list == null) this.clearObservers();
			return this.list;
		}
		
		public function removeObserver(observer:IFObserver):Boolean
		{
			if(this.list == null) return false;
			var boo:Boolean = this.list.removeItem(observer);
			return boo;
		}
		
		public function clearObservers():void
		{
			this.list = new Collection();
		}
		
		public function notifyObservers(obj:Object = null):void
		{	
			if(! this.hasChanged()|| this.list == null) return;
			var dupe:IFCollection = this.list.concat();
			var it:IFIterator = dupe.getIterator();
			while(it.hasNext()){
				var observer:IFObserver = IFObserver(it.next());
				observer.update(obj);
			}
			this.clearChanged();
		}
		
		public function hasChanged():Boolean
		{
			return this.changed;
		}
		
		public function setChanged():void
		{
			this.changed = new Boolean(true);
		}
		
		public function clearChanged():void
		{
			this.changed = new Boolean();
		}
	}
}